<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$utility_id = $_REQUEST["utility_id"];
	if (isset($utility_id)) {
		$sql = "select * from tbl_utility where utility_id = '$utility_id'";
		$db = NEW myDBs();
		$db->selectDB("account");

		$result = $db->runSQL($sql);
		$rows = $db->fetch_object($result);

		$panngan2 = $rows->panngan2_id;
		$panngan3 = $rows->panngan3_id;

		$budget_year = $rows->budget_year;

		$month_year = $rows->month_year;
		
		$ex1_id = $rows->ex1_id;
		$ex2_id = $rows->ex2_id;

		$amount = $rows->utility;


	}
	

	//$ex3_id = $_REQUEST["ex3_id"];

	
?>

<script type="text/javascript">
	$(document).ready(function(){

		$.post('dropdown.php', {  tablename: 'expenses_lvl1', dd: 'ex1_id', idname: 'expenses1_id', fieldname: 'expenses1_name', by: '05', ex1_id:'<?php echo $ex1_id; ?>', ex2_id:'<?php echo $ex2_id; ?>', ex3_id:'<?php echo $ex3_id; ?>'  }, function(data) { $("#expenses1").html(data); } );

		$("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
			submitSuccess: function ($form, event) { 
				event.preventDefault();
				
				$.ajax({
					type: "POST",
					url: "process_utility.php",
					data: $('#frm_petition_utility').serialize(),
					cach: false,
					error: function() {
						alert("ERROR !!!!");
					},
					success: function(data){
						//$("#kob").after(data);
						//$("#test").html(data);
						window.location.href="index.php?pages=frm_petition_utility&pageid=9";

					}
				});
			}
		});

		$( "#panngan2" ).change(function() {
			var panngan2= $(this).val();
			var panngan3 = "<?php echo $panngan3; ?>";
			
			//alert(panngan2);
			
			if (panngan2 != "") {
				//alert(panngan2);
				$.post('dd_panngan.php', {  panngan2: panngan2, panngan3: panngan3}, function(data) { 
					$("#dd_panngan3").html(data); 

				} );
			}

		});

		$("#month_year").datepicker({

			dateFormat: "yy-mm"
			//defaultDate: new Date()
			//showMonthAfterYear:true,
			
		});
		
		$('#panngan2').trigger('change');

	}); //(document).ready

	
</script>
<div id="test"></div>
<form id="frm_petition_utility" name="frm_petition_utility" method="post" action="index.php" novalidate="">
	<input type="hidden" name="utility_id" value="<?php echo $utility_id; ?>">
	<input type="hidden" name="task" value="<?php echo isset($utility_id)? "edit":"new"; ?>">
	
	<div class="row-fluid">
		<div class="control-group span2">
			<label class="control-label" for="budget_year">ปีงบประมาณ</label>
			<div class="controls ">
				<select class="span12" id="budget_year" name="budget_year" readonly>
					<option>2556</option>
					<option>2557</option>
					<option>2558</option>
					<option>2559</option>
					<option>2560</option>
				</select>
			</div>
		</div>

		<div class="control-group span5">
			<label class="control-label" for="panngan">แผนงาน</label>
			<div class="controls ">
				<select class="span12" id="panngan2" name="panngan2" required>
					<option value="">เลือก...</option>
				<?php


					$db = NEW myDBS;
					$db->selectDB("hr_udoncity");
					
					$sql = "SELECT DISTINCT panngan2_id 
								FROM position 
								WHERE lvl_id like '" .$_SESSION["lvl1_id"]. "%'
								AND panngan2_id IS NOT NULL"
								; 

					//echo $sql;

					$result = $db->runSQL($sql);
					
					while($rows = $db->fetch_object ( $result )){
						$db->selectDB("account");
						$sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" .$rows->panngan2_id. "'";
						$result1 = $db->runSQL($sql);
						while($rows1 = $db->fetch_object ( $result1 )){
							if ($rows1->panngan2_id == $panngan2) {
								$selected = " selected";

							} else {
								$selected = "";
							}
							echo "<option value='".$rows1->panngan2_id."' $selected>$rows1->panngan2_name</option>";
						}
					}

				?>
				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span5">
			<label class="control-label" for="panngan3">งาน</label>
			<div class="controls " id="dd_panngan3">
				<select class="span12" id="panngan3" name="panngan3">
					<option value="">เลือก</option>

				</select>
			</div>
		</div><!--/span-->
	</div><!--/row-fluid-->
	<div class="row-fluid">
		<div class="control-group span6">
			<label class="control-label" for="expenses1_name">หมวด / ลักษณะ</label>
			<div id="expenses1" class="controls">
				<select class="span12" id="ex1_id" name="ex1_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		<div class="control-group span6">
			<label class="control-label" for="expenses2_name">ประเภท</label>
			<div id="expenses2" class="controls">
				<select class="span12" id="ex2_id" name="ex2_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>
		<!--
		<div class="control-group span4">
			<label class="control-label" for="expenses3_name">รายการ</label>
			<div id="expenses3" class="controls">
				<select class="span12" id="ex3_id" name="ex3_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>
		-->
	</div><!--/row-fluid-->
	<div class="row-fluid">
		<div class="control-group span4">
			<label class="control-label" for="month_year">เดือน/ปี</label>
			<div class="controls">
				<input class="span12" type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" required />
            </div>
		</div>
		<div class="control-group span4">
			<label class="control-label" for="amount">จำนวนเงิน</label>
			<div class="controls">
				<input class="span12" type="text" id="amount" name="amount" value="<?php echo $amount; ?>" required />
            </div>
		</div>

	</div><!--/row-fluid-->
	
	<div class="form-actions">

		<label class="control-label" for="blank">&nbsp;</label>
			<button type="submit" id="submit" name="submit"  class="btn btn-primary"><i class="icon-ok icon-white"></i> บันทึกข้อมูล</button>
			<a class="btn close-dialog"><i class="icon-remove"></i> ยกเลิก</a>
	</div>

</form> 
